// 定义球队类
class Team {
    constructor(name) {
      this.name = name;       // 队名
      this.played = 0;         // 已比赛场数
      this.won = 0;            // 赢的场数
      this.drawn = 0;          // 平的场数
      this.lost = 0;           // 负的场数
      this.goalsFor = 0;       // 进球数
      this.goalsAgainst = 0;   // 失球数
      this.points = 0;         // 积分
    }
  
    // 更新比赛结果
    updateStats(goalsFor, goalsAgainst) {
      this.played += 1;
      this.goalsFor += goalsFor;
      this.goalsAgainst += goalsAgainst;
  
      if (goalsFor > goalsAgainst) {
        this.won += 1;
        this.points += 3;
      } else if (goalsFor < goalsAgainst) {
        this.lost += 1;
      } else {
        this.drawn += 1;
        this.points += 1;
      }
    }
  
    // 计算净胜球
    get goalDifference() {
      return this.goalsFor - this.goalsAgainst;
    }
  
    // 打印球队信息
    print() {
      console.log(`${this.name} | ${this.played} | ${this.won} | ${this.drawn} | ${this.lost} | ${this.goalsFor} | ${this.goalsAgainst} | ${this.points}`);
    }
  }
  
  // 排序规则：先按积分、再按净胜球、最后按进球数排序
  function compareTeams(teamA, teamB) {
    if (teamA.points !== teamB.points) {
      return teamB.points - teamA.points;
    }
    if (teamA.goalDifference !== teamB.goalDifference) {
      return teamB.goalDifference - teamA.goalDifference;
    }
    return teamB.goalsFor - teamA.goalsFor;
  }
  
  // 更新积分表
  function updateLeague(teams, homeTeam, awayTeam, homeGoals, awayGoals) {
    // 找到主队和客队
    let home = teams.find(team => team.name === homeTeam);
    let away = teams.find(team => team.name === awayTeam);
  
    // 更新两队的统计
    home.updateStats(homeGoals, awayGoals);
    away.updateStats(awayGoals, homeGoals);
  }
  
  // 打印积分榜
  function printLeague(teams) {
    // 排序
    teams.sort(compareTeams);
    
    // 打印表头
    console.log('队名 | 已赛 | 胜 | 平 | 负 | 进球 | 失球 | 积分');
    console.log('-------------------------------------------------');
    
    // 打印球队信息
    teams.forEach(team => team.print());
  }
  
  // 示例：创建几个球队并更新积分
  const teams = [
    new Team('Team A'),
    new Team('Team B'),
    new Team('Team C'),
    new Team('Team D')
  ];
  
  // 模拟比赛
  updateLeague(teams, 'Team A', 'Team B', 2, 1);
  updateLeague(teams, 'Team C', 'Team D', 0, 0);
  updateLeague(teams, 'Team A', 'Team C', 3, 1);
  updateLeague(teams, 'Team B', 'Team D', 1, 1);
  
  // 打印最新积分榜
  printLeague(teams);
  